package com.www.applyword.ui.my;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import androidx.core.content.FileProvider;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.lljjcoder.Interface.OnCityItemClickListener;
import com.lljjcoder.bean.CityBean;
import com.lljjcoder.bean.DistrictBean;
import com.lljjcoder.bean.ProvinceBean;
import com.lljjcoder.citywheel.CityConfig;
import com.lljjcoder.style.citypickerview.CityPickerView;
import com.www.applyword.Constants.Constants;
import com.www.applyword.R;
import com.www.applyword.base.BaseActivity;
import com.www.applyword.bean.CertificationBean;
import com.www.applyword.bean.LoginBean;
import com.www.applyword.http.CallBack;
import com.www.applyword.http.HttpClient;
import com.www.applyword.utils.ImageUtil;
import com.www.applyword.utils.PhotoUtil;
import com.www.applyword.utils.SPUtil;
import com.www.applyword.utils.Utils;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import cn.bingoogolapple.photopicker.activity.BGAPhotoPickerActivity;
import cn.bingoogolapple.photopicker.activity.BGAPhotoPickerPreviewActivity;
import cn.bingoogolapple.photopicker.widget.BGASortableNinePhotoLayout;
import de.hdodenhof.circleimageview.CircleImageView;

import static com.www.applyword.ui.chat.FabuDynaminActivity.RC_CHOOSE_PHOTO;
import static com.www.applyword.ui.chat.FabuDynaminActivity.RC_CHOOSE_PHOTO2;
import static com.www.applyword.ui.chat.FabuDynaminActivity.RC_PHOTO_PREVIEW;
import static com.www.applyword.ui.chat.FabuDynaminActivity.RC_PHOTO_PREVIEW2;
import static com.www.applyword.ui.my.PersonalActivity.CODE_CAMERA_REQUEST;
import static com.www.applyword.ui.my.PersonalActivity.CODE_GALLERY_REQUEST;
import static com.www.applyword.ui.my.PersonalActivity.CODE_RESULT_REQUEST;
import static com.www.applyword.ui.my.PersonalActivity.OUTPUT_X;
import static com.www.applyword.ui.my.PersonalActivity.OUTPUT_Y;

/**
 * author : huang
 * date   : 2019/10/23 0023 15:47
 * version: 1.0
 */
public class MyEnterpriseActivity extends BaseActivity implements View.OnClickListener, BGASortableNinePhotoLayout.Delegate {
    @BindView(R.id.civ_head_photo)
    CircleImageView civHeadPhoto;
    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.et_name2)
    EditText etName2;
    @BindView(R.id.et_code)
    EditText etCode;
    @BindView(R.id.tv_profession)
    TextView tvProfession;
    @BindView(R.id.tv_business_type)
    TextView tvBusinessType;
    @BindView(R.id.tv_staff_size)
    TextView tvStaffSize;
    @BindView(R.id.tv_address)
    TextView tvAddress;
    @BindView(R.id.et_adress_info)
    EditText etAdressInfo;
    @BindView(R.id.et_email)
    EditText etEmail;
    @BindView(R.id.et_qq)
    EditText etQq;
    @BindView(R.id.et_weixin)
    EditText etWeixin;
    @BindView(R.id.snpl_moment_add_photos)
    BGASortableNinePhotoLayout mPhotosSnpl;
    @BindView(R.id.snpl_incumbency_certification)
    BGASortableNinePhotoLayout snplIncumbencyCertification;
    @BindView(R.id.tv_num)
    TextView tvNum;
    @BindView(R.id.et_info)
    EditText etInfo;

    private List<String> gradeData = new ArrayList<>(); //自行添加一些string
    private ArrayList<String> list_path = new ArrayList<>();
    private ArrayList<String> list_path2 = new ArrayList<>();
    private List<String> data;
    private List<String> data2;

    OptionsPickerView pvOptions;
    private String typeName;
    private Dialog dialog, dialogHead;

    private Uri imageUri;
    private Uri cropImageUri;
    private File fileUri = new File(Environment.getExternalStorageDirectory().getPath() + "/photo.jpg");
    private File fileCropUri = new File(Environment.getExternalStorageDirectory().getPath() + "/crop_photo.jpg");
    private boolean flag;
    private String url;

    private CertificationBean.CertificationBean2 certification;


    @Override
    protected void loadViewLayout() {
        setContentView(R.layout.enterprise_activity);
        setTitle("企业认证");
        setRringTitle("提交");
        mPhotosSnpl.setDelegate(this);
      //  snplIncumbencyCertification.setDelegate(new );
        snplIncumbencyCertification.setDelegate(new BGASortableNinePhotoLayout.Delegate() {
            @Override
            public void onClickAddNinePhotoItem(BGASortableNinePhotoLayout sortableNinePhotoLayout, View view, int position, ArrayList<String> models) {
                choicePhotoWrapper2();
            }

            @Override
            public void onClickDeleteNinePhotoItem(BGASortableNinePhotoLayout sortableNinePhotoLayout, View view, int position, String model, ArrayList<String> models) {
                if (snplIncumbencyCertification.getData().size() > 0) {
                    snplIncumbencyCertification.removeItem(position);
                }
            }

            @Override
            public void onClickNinePhotoItem(BGASortableNinePhotoLayout sortableNinePhotoLayout, View view, int position, String model, ArrayList<String> models) {
                Intent photoPickerPreviewIntent = new BGAPhotoPickerPreviewActivity.IntentBuilder(MyEnterpriseActivity.this)
                        .previewPhotos(models) // 当前预览的图片路径集合
                        .selectedPhotos(models) // 当前已选中的图片路径集合
                        .maxChooseCount(snplIncumbencyCertification.getMaxItemCount()) // 图片选择张数的最大值
                        .currentPosition(position) // 当前预览图片的索引
                        .isFromTakePhoto(false) // 是否是拍完照后跳转过来
                        .build();
                startActivityForResult(photoPickerPreviewIntent, RC_PHOTO_PREVIEW2);
            }

            @Override
            public void onNinePhotoItemExchanged(BGASortableNinePhotoLayout sortableNinePhotoLayout, int fromPosition, int toPosition, ArrayList<String> models) {

            }
        });
    }

    private void choicePhotoWrapper2() {
        File takePhotoDir = new File(Environment.getExternalStorageDirectory(), "job");
        Intent photoPickerIntent = new BGAPhotoPickerActivity.IntentBuilder(this)
                .cameraFileDir(takePhotoDir) // 拍照后照片的存放目录，改成你自己拍照后要存放照片的目录。如果不传递该参数的话则不开启图库里的拍照功能
                .maxChooseCount(snplIncumbencyCertification.getMaxItemCount() - snplIncumbencyCertification.getItemCount()) // 图片选择张数的最大值
                .selectedPhotos(null) // 当前已选中的图片路径集合
                .pauseOnScroll(false) // 滚动列表时是否暂停加载图片
                .build();
        startActivityForResult(photoPickerIntent, RC_CHOOSE_PHOTO2);
    }

    @Override
    protected void loadData() {
        String user_id = getIntent().getStringExtra("user_id");
        int type = getIntent().getIntExtra("type",0);
        if (type == 1){
            tv_right.setVisibility(View.GONE);
        }
        Map<String, Object> params = new HashMap<>();
        if (TextUtils.isEmpty(user_id)){
            params.put("UserID",SPUtil.getString(Constants.USERID));
        }else {
            params.put("UserID",user_id);
        }

        HttpClient.get(MyEnterpriseActivity.this, Constants.GETENTERPRISECERTIFICATION, params,new CallBack<CertificationBean>() {
            @Override
            public void onSuccess(CertificationBean result) {
                certification = result.getCertification();
                etName.setText(result.getCertification().getEnterpriseCertification_Name());
                etName2.setText(result.getCertification().getEnterpriseCertification_LegalPerson());
                etCode.setText(result.getCertification().getEnterpriseCertification_CreditCode());
                tvProfession.setText(result.getCertification().getEnterpriseCertification_IndustryName());
                tvBusinessType.setText(result.getCertification().getEnterpriseCertification_EnterpriseTypeName());
                tvStaffSize.setText(result.getCertification().getEnterpriseCertification_People());
                tvAddress.setText(result.getCertification().getEnterpriseCertification_Address());
                etInfo.setText(result.getCertification().getEnterpriseCertification_Introductions());
                etAdressInfo.setText((CharSequence) result.getCertification().getEnterpriseCertification_RegisterAddress());
                ImageUtil.setImage(civHeadPhoto,result.getCertification().getEnterpriseCertification_Logo());
                ArrayList<String> list = new ArrayList<>();
                //企业执照
                list.add(Constants.HOST + result.getCertification().getEnterpriseCertification_BusinessLicense());
                mPhotosSnpl.addMoreData(list);
                flag = true;

                ArrayList<String> list2 = new ArrayList<>();
                String EnterpriseCertification_EmpowerPeople =  result.getCertification().getEnterpriseCertification_EmpowerPeople();
                if (EnterpriseCertification_EmpowerPeople.contains(",")){
                    String[] split = EnterpriseCertification_EmpowerPeople.split(",");
                    for (int i = 0; i < split.length; i++) {
                        list2.add(Constants.HOST + split[i]);
                    }
                    snplIncumbencyCertification.addMoreData(list2);
                }else {
                    list2.add(Constants.HOST + EnterpriseCertification_EmpowerPeople);
                    snplIncumbencyCertification.addMoreData(list2);
                }
            }
        });


        if (Utils.isFastClick()) {
            tv_right.setOnClickListener(v -> {
                if (!flag) {
                    Utils.showToastSafe("请上传企业头像");
                    return;
                }
                if (TextUtils.isEmpty(etName.getText().toString().trim())) {
                    Utils.showToastSafe(etName.getHint().toString());
                    return;
                }
                if (TextUtils.isEmpty(etName2.getText().toString().trim())) {
                    Utils.showToastSafe(etName2.getHint().toString());
                    return;
                }

                if (TextUtils.isEmpty(etCode.getText().toString().trim())) {
                    Utils.showToastSafe(etCode.getHint().toString());
                    return;
                }
                if (TextUtils.isEmpty(tvProfession.getText().toString().trim())) {
                    Utils.showToastSafe(tvProfession.getHint().toString());
                    return;
                }
                if (TextUtils.isEmpty(tvBusinessType.getText().toString().trim())) {
                    Utils.showToastSafe(tvBusinessType.getHint().toString());
                    return;
                }
                if (TextUtils.isEmpty(tvStaffSize.getText().toString().trim())) {
                    Utils.showToastSafe(tvStaffSize.getHint().toString());
                    return;
                }
                if (TextUtils.isEmpty(tvAddress.getText().toString().trim())) {
                    Utils.showToastSafe(tvAddress.getHint().toString());
                    return;
                }
                if (TextUtils.isEmpty(etAdressInfo.getText().toString().trim())) {
                    Utils.showToastSafe(etAdressInfo.getHint().toString());
                    return;
                }
                if (TextUtils.isEmpty(etInfo.getText().toString().trim())) {
                    Utils.showToastSafe(etInfo.getHint().toString());
                    return;
                }

                if (mPhotosSnpl.getData().size() == 0) {
                    Utils.showToastSafe("请先上传营业执照");
                    return;
                }
                if (snplIncumbencyCertification.getData().size() == 0) {
                    Utils.showToastSafe("请先上传在职证明");
                    return;
                }

                list_path.clear();
                list_path2.clear();
                data = mPhotosSnpl.getData();
                data2 = snplIncumbencyCertification.getData();

                if (null == cropImageUri){
                    if (null != certification)
                    url = certification.getEnterpriseCertification_Logo();
                }else {
                    HttpClient.postImg(MyEnterpriseActivity.this, Constants.UPLOADENTERPRISECERTIFICATION, fileCropUri, new CallBack<List<String>>() {
                        @Override
                        public void onSuccess(List<String> list) {
                            url = list.get(0);
                        }
                    });
                }

                for (String datum : mPhotosSnpl.getData()) {
                    if (datum.contains("http")) {
                        list_path.add(datum);
                    } else {
                        HttpClient.postImg(MyEnterpriseActivity.this, Constants.UPLOADENTERPRISECERTIFICATION, new File(datum), new CallBack<List<String>>() {
                            @Override
                            public void onSuccess(List<String> list) {
                                list_path.add(list.get(0));
                            }
                        });
                    }
                }

                for (String datum : snplIncumbencyCertification.getData()) {
                    if (datum.contains("http")) {
                        list_path2.add(datum);
                        if (data2.size() == list_path2.size()) {
                            getUpdate();
                        }
                    } else {
                        HttpClient.postImg(MyEnterpriseActivity.this, Constants.UPLOADENTERPRISECERTIFICATION, new File(datum), new CallBack<List<String>>() {
                            @Override
                            public void onSuccess(List<String> list) {
                                list_path2.add(list.get(0));
                                if (data2.size() == list_path2.size()) {
                                    getUpdate();
                                }
                            }
                        });
                    }
                }


                  /*  HttpClient.postImg(MyEnterpriseActivity.this, Constants.UPLOADENTERPRISECERTIFICATION, new File(datum), new CallBack<List<String>>() {
                        @Override
                        public void onSuccess(List<String> list) {
                            list_path.add(list.get(0));
                            if (data.size() == list_path.size()) {
                                for (String datum : snplIncumbencyCertification.getData()) {
                                    HttpClient.postImg(MyEnterpriseActivity.this, Constants.UPLOADENTERPRISECERTIFICATION, new File(datum), new CallBack<List<String>>() {
                                        @Override
                                        public void onSuccess(List<String> list) {
                                            list_path2.add(list.get(0));
                                            if (data2.size() == list_path2.size()) {
                                                getUpdate();
                                            }
                                        }
                                    });
                                }
                            }
                        }
                    });
                }*/
            });
        }
    }

    private void getUpdate() {
         String photosUrl = "";
         String photosUrl2 = "";
        for (String s : list_path) {
            String s1;
            if (s.contains("http")){
               s1 = s.split(Constants.HOST)[1];
            }else {
                s1 = s;
            }
            photosUrl += s1 + ",";
        }

        for (String s : list_path2) {
            String s2;
            if (s.contains("http")){
                s2 = s.split(Constants.HOST)[1];
            }else {
                s2 = s;
            }
            photosUrl2 += s2 + ",";
        }

        Map<String, Object> params = new HashMap<>();
        params.put("EnterpriseCertification_UserID", SPUtil.getString(Constants.USERID));
        params.put("EnterpriseCertification_Name",etName.getText().toString().trim());
        params.put("EnterpriseCertification_LegalPerson", etName2.getText().toString().trim());
        params.put("EnterpriseCertification_CreditCode", etCode.getText().toString().trim());
        params.put("EnterpriseCertification_IndustryName", tvProfession.getText().toString());
        params.put("EnterpriseCertification_EnterpriseTypeName", tvBusinessType.getText().toString());
        params.put("EnterpriseCertification_People", tvStaffSize.getText().toString().trim());
        params.put("EnterpriseCertification_Address", tvAddress.getText().toString().trim());
        params.put("EnterpriseCertification_RegisterAddress", etAdressInfo.getText().toString().trim());
        //营业执照
        params.put("EnterpriseCertification_BusinessLicense", photosUrl.substring(0,photosUrl.length()-1));
        //授权员工
        params.put("EnterpriseCertification_EmpowerPeople", photosUrl2.substring(0,photosUrl.length()-1));
        params.put("EnterpriseCertification_Introductions",etInfo.getText().toString().trim());
        params.put("EnterpriseCertification_Logo", url);
        HttpClient.post(MyEnterpriseActivity.this, Constants.ENTERPRISECERTIFICATION, params, new CallBack<LoginBean>() {
            @Override
            public void onSuccess(LoginBean result) {
                Utils.showToastSafe("提交成功");
                finish();
            }
        });
    }


    @OnClick({R.id.tv_profession, R.id.tv_business_type, R.id.tv_staff_size, R.id.tv_address, R.id.snpl_moment_add_photos,
            R.id.snpl_incumbency_certification,R.id.ll_head_photo})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            //所属行业
            case R.id.tv_profession:
                typeName = "所属行业";
                showSex(1);
                break;
            case R.id.tv_business_type:
                 typeName = "企业类型";
                showSex(2);
                break;
            case R.id.tv_staff_size:
                 typeName = "人员规模";
                showSex(3);
                break;
            case R.id.tv_address:
                showAddress();
                break;
            case R.id.snpl_moment_add_photos:
                break;
            case R.id.snpl_incumbency_certification:
                break;
            case R.id.ll_head_photo:
                showHeadDialog();
                break;
                default:
        }
    }

    private void showHeadDialog() {
        dialogHead = new Dialog(this, R.style.ActionSheetDialogStyle);
        View view = LayoutInflater.from(this).inflate(R.layout.dialog_head_portrait, null);
        view.findViewById(R.id.dialog_shoot).setOnClickListener(this);
        view.findViewById(R.id.dialog_photo_album).setOnClickListener(this);
        view.findViewById(R.id.dialog_cancel).setOnClickListener(this);
        dialogHead.setContentView(view);
        Window window = dialogHead.getWindow();
        if (window == null) {
            return;
        }
        dialogHead.show();//显示对话框//设置Dialog从窗体底部弹出
        window.setGravity(Gravity.BOTTOM);
        //获取窗体的属性
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.y = 20;//设置Dialog距离底部的距离
        //将属性设置给窗体
        window.setAttributes(lp);
    }

    private void showAddress() {
        hideInput2();
        //申明对象
        CityPickerView mPicker = new CityPickerView();
        //预先加载仿iOS滚轮实现的全部数据（oncreate()中）
        mPicker.init(this);

        //配置
        CityConfig cityConfig = new CityConfig.Builder()
                .title("选择城市")//标题
                .titleTextSize(10)//标题文字大小
                .titleTextColor("#585858")//标题文字颜  色
                .titleBackgroundColor("#ffdddddd")//标题栏背景色
                .confirTextColor("#ffe1163c")//确认按钮文字颜色
                .confirmText("确定")//确认按钮文字
                .confirmTextSize(10)//确认按钮文字大小
                .cancelTextColor("#585858")//取消按钮文字颜色
                .cancelText("取消")//取消按钮文字
                .cancelTextSize(10)//取消按钮文字大小
                .setCityWheelType(CityConfig.WheelType.PRO_CITY_DIS)//显示类，只显示省份一级，显示省市两级还是显示省市区三级
                .showBackground(true)//是否显示半透明背景
                .visibleItemsCount(7)//显示item的数量
                .province("湖北省")//默认显示的省份
                .city("武汉市")//默认显示省份下面的城市
                .district("洪山区")//默认显示省市下面的区县数据
                .provinceCyclic(true)//省份滚轮是否可以循环滚动
                .cityCyclic(true)//城市滚轮是否可以循环滚动
                .districtCyclic(true)//区县滚轮是否循环滚动
                .setCustomItemLayout(R.layout.item_city)//自定义item的布局
                .setCustomItemTextViewId(R.id.item_city_name_tv)//自定义item布局里面的textViewid
                .drawShadows(false)//滚轮不显示模糊效果
                .setLineHeigh(1)//中间横线的高度
                .setLineColor("#fff9c21a")
                .setShowGAT(true)//是否显示港澳台数据，默认不显示
                .build();
        //添加配置
        mPicker.setConfig(cityConfig);
        //显示
        mPicker.showCityPicker();
        //监听选择点击事件及返回结果
        mPicker.setOnCityItemClickListener(new OnCityItemClickListener() {
            @Override
            public void onSelected(ProvinceBean province, CityBean city, DistrictBean district) {
                super.onSelected(province, city, district);
                //为TextView赋值
                tvAddress.setText(province + "-" + city + "-" + district);
              String  address = province + "," + city + "," + district;
            }

            @Override
            public void onCancel() {
                super.onCancel();
            }
        });
    }

    private void showSex(int type) {
        hideInput2();
        if (type == 1) {
            gradeData.clear();
            gradeData.add("IT/互联网/网络");
            gradeData.add("人力资源");
            gradeData.add("服务业");
            gradeData.add("百货/零售");
            gradeData.add("餐饮/旅游/娱乐");
            gradeData.add("贸易/物流");
            gradeData.add("办公文教");
            gradeData.add("广告");
            gradeData.add("房地产");
            gradeData.add("影视/媒体");
            gradeData.add("物业管理");
            gradeData.add("物业管理");
            gradeData.add("医药");
            gradeData.add("通信");
            gradeData.add("其他行业");
        } else if (type == 2) {
            gradeData.clear();
            gradeData.add("有限责任公司");
            gradeData.add("股份有限公司");
            gradeData.add("集体所有制");
            gradeData.add("国有企业");
            gradeData.add("个人独资企业");
            gradeData.add("有限合伙");
            gradeData.add("普通合伙");
            gradeData.add("外商投资企业");
            gradeData.add("私营企业");
        } else if (type == 3) {
            gradeData.clear();
            gradeData.add("小于50人");
            gradeData.add("50-99人");
            gradeData.add("100-499人");
            gradeData.add("1000-4999年");
            gradeData.add("5000-9999年");
            gradeData.add("100000年");
        } else if (type == 4) {
            gradeData.clear();
            gradeData.add("不限");
            gradeData.add("18-30岁");
            gradeData.add("30-40岁");
            gradeData.add("50岁以下");
        }else {
            gradeData.clear();
            gradeData.add("全职");
            gradeData.add("兼职");
        }
        pvOptions = new OptionsPickerBuilder(this, (options1, options2, options3, v) -> {
            if (type == 1) {
                tvProfession.setText(gradeData.get(options1));
            } else if (type == 2) {
                //学历
                tvBusinessType.setText(gradeData.get(options1));
            } else if (type == 3) {
                tvStaffSize.setText(gradeData.get(options1));
            } else if (type == 4) {

            }else {
              // tvOrderPositionType.setText(gradeData.get(options1));
            }
        })
                .setTitleText(typeName)
                .setSubmitText("确定")//确定按钮文字
                .setCancelText("取消")//取消按钮文字
                .setSubCalSize(10)//确定和取消文字大小
                .setSubmitColor(0xffFF6767)//确定按钮文字颜色
                .setCancelColor(0xff999999)//取消按钮文字颜色
                .setTitleBgColor(0xffF4F4F4)//标题背景颜色 Night mode
                .setTitleSize(10)
                .setContentTextSize(12)//滚轮文字大小
                .setTextColorCenter(0xff333333)
                .setTextColorOut(0x999999)
                .setDividerColor(0xffEEEEEE)
                .setCyclic(false, false, false)//循环与否
                .setSelectOptions(0, 0, 0)  //设置默认选中项
                .setOutSideCancelable(true)//点击外部dismiss default true
                .isDialog(false)//是否显示为对话框样式
                .isRestoreItem(true)//切换时是否还原，设置默认选中第一项。
                .setLineSpacingMultiplier((float) 2.5) //设置item的高度
                .build();
        pvOptions.setPicker(gradeData);
        pvOptions.show();


    }

    private void hideInput2() {
        InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
        View v = getWindow().peekDecorView();
        if (null != v) {
            imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.dialog_shoot://拍摄
                imageUri = Uri.fromFile(fileUri);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                    imageUri = FileProvider.getUriForFile(this, "com.whmnrc.parttimejob.FileProvider", fileUri);
                }
                PhotoUtil.takePicture(this, imageUri, CODE_CAMERA_REQUEST);
                dialogHead.dismiss();

                break;
            case R.id.dialog_photo_album://相册
                PhotoUtil.openPic(this, CODE_GALLERY_REQUEST);
                dialogHead.dismiss();
                break;

            case R.id.dialog_cancel://取消
                dialogHead.dismiss();
                break;

            default:
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case CODE_CAMERA_REQUEST:
                    cropImageUri = Uri.fromFile(fileCropUri);
                    PhotoUtil.cropImageUri(this, imageUri, cropImageUri, 1, 1, OUTPUT_X, OUTPUT_Y, CODE_RESULT_REQUEST);
                    break;
                case CODE_GALLERY_REQUEST:
                    cropImageUri = Uri.fromFile(fileCropUri);
                    Uri newUri = Uri.parse(PhotoUtil.getPath(this, intent.getData()));
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                        newUri = FileProvider.getUriForFile(this, "com.whmnrc.parttimejob.FileProvider", new File(newUri.getPath()));
                    }
                    PhotoUtil.cropImageUri(this, newUri, cropImageUri, 1, 1, OUTPUT_X, OUTPUT_Y, CODE_RESULT_REQUEST);
                    break;
                case CODE_RESULT_REQUEST:
                    Bitmap bitmap = PhotoUtil.getBitmapFromUri(cropImageUri, this);
                    if (bitmap != null) {
                        flag = true;
                        civHeadPhoto.setImageBitmap(bitmap);
                    }
                    break;
                default:
            }
        }

        if (resultCode == RESULT_OK && requestCode == RC_CHOOSE_PHOTO) { //拍照
            mPhotosSnpl.addMoreData(BGAPhotoPickerActivity.getSelectedPhotos(intent));
        } else if (requestCode == RC_PHOTO_PREVIEW) {   //相册
            mPhotosSnpl.setData(BGAPhotoPickerPreviewActivity.getSelectedPhotos(intent));
        }else if (requestCode == RC_PHOTO_PREVIEW2) {   //相册
            snplIncumbencyCertification.setData(BGAPhotoPickerPreviewActivity.getSelectedPhotos(intent));
        }else if (requestCode == RC_CHOOSE_PHOTO2){
            snplIncumbencyCertification.addMoreData(BGAPhotoPickerActivity.getSelectedPhotos(intent));
        }

        super.onActivityResult(requestCode, resultCode, intent);
    }

    @Override
    public void onClickAddNinePhotoItem(BGASortableNinePhotoLayout sortableNinePhotoLayout, View view, int position, ArrayList<String> models) {
        choicePhotoWrapper();
    }

    private void choicePhotoWrapper() {
        // 拍照后照片的存放目录，改成你自己拍照后要存放照片的目录。如果不传递该参数的话就没有拍照功能
        File takePhotoDir = new File(Environment.getExternalStorageDirectory(), "job");

        Intent photoPickerIntent = new BGAPhotoPickerActivity.IntentBuilder(this)
                .cameraFileDir(takePhotoDir) // 拍照后照片的存放目录，改成你自己拍照后要存放照片的目录。如果不传递该参数的话则不开启图库里的拍照功能
                .maxChooseCount(mPhotosSnpl.getMaxItemCount() - mPhotosSnpl.getItemCount()) // 图片选择张数的最大值
                .selectedPhotos(null) // 当前已选中的图片路径集合
                .pauseOnScroll(false) // 滚动列表时是否暂停加载图片
                .build();
        startActivityForResult(photoPickerIntent, RC_CHOOSE_PHOTO);


    }

    @Override
    public void onClickDeleteNinePhotoItem(BGASortableNinePhotoLayout sortableNinePhotoLayout, View view, int position, String model, ArrayList<String> models) {
        if (mPhotosSnpl.getData().size() > 0) {
            mPhotosSnpl.removeItem(position);
        }

    }

    @Override
    public void onClickNinePhotoItem(BGASortableNinePhotoLayout sortableNinePhotoLayout, View view, int position, String model, ArrayList<String> models) {
        Intent photoPickerPreviewIntent = new BGAPhotoPickerPreviewActivity.IntentBuilder(this)
                .previewPhotos(models) // 当前预览的图片路径集合
                .selectedPhotos(models) // 当前已选中的图片路径集合
                .maxChooseCount(mPhotosSnpl.getMaxItemCount()) // 图片选择张数的最大值
                .currentPosition(position) // 当前预览图片的索引
                .isFromTakePhoto(false) // 是否是拍完照后跳转过来
                .build();
        startActivityForResult(photoPickerPreviewIntent, RC_PHOTO_PREVIEW);

    }

    @Override
    public void onNinePhotoItemExchanged(BGASortableNinePhotoLayout sortableNinePhotoLayout, int fromPosition, int toPosition, ArrayList<String> models) {

    }
}
